//
//  KDSecondNotyController.m
//  WangJingApp
//
//  Created by 魏强 on 17/2/8.
//  Copyright © 2017年 KDTech. All rights reserved.
//  发通知中选择发送范围和公告类型的界面

#import "KDSecondNotyController.h"
#import "KDThirdNotyController.h"
@interface KDSecondNotyController ()<UITableViewDelegate, UITableViewDataSource, KDThirdNotyControllerDelegate>
/**  <#Description#> */
@property (nonatomic, weak) UITableView *tableView;
/**  公告类型 */
@property (nonatomic, copy) NSString * notyType;
/**  发送单位数 */
@property (nonatomic, copy) NSString * notyNmu;

/**  公告类型合集 */
@property (nonatomic, strong) NSArray *types;
/**  发送按钮 */
@property (nonatomic, weak) UIButton *confirmBtn;

@end

@implementation KDSecondNotyController
#pragma mark - 懒加载区
- (NSArray *)types
{
    if (!_types) {
        _types = @[@"选项一",@"选项二",@"选项三",@"选项四"] ;
    }
    return _types;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.notyType = @"未选择";
    self.notyNmu = @"0单位";
    self.view.backgroundColor = BackGroundColor;
    [self setupTableView];
    [self setupConfirmBtn];
}
#pragma mark - 板块布置区
- (void)setupTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , 0, screenW,  self.view.height *0.7) style:UITableViewStyleGrouped];
    tableView.contentInset = UIEdgeInsetsMake(-30, 0, 0, 0);
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    //    tableView.scrollEnabled = NO;
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    tableView.backgroundColor = BackGroundColor;
    [self.view addSubview:tableView];

}

- (void)setupConfirmBtn{
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(10, CGRectGetMaxY(self.tableView.frame) + 20, screenW - 20, 44);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    confirmBtn.backgroundColor = KDTintColor;
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];;
    self.confirmBtn = confirmBtn;
    [self.view addSubview:confirmBtn];
}
#pragma mark - 事件点击区
- (void)confirmBtnClick{
    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}
#pragma mark - 网络请求区

#pragma mark - 通知接收区

#pragma mark - UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return 5;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId =@"secondNotyCell";
    if (indexPath.section == 0) {
         UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@1", cellId]];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"%@1", cellId]];
            cell.textLabel.text = @"发送范围";
            
            cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        }
        cell.detailTextLabel.text = self.notyNmu;
        return cell;
    }else{
        if (indexPath.row == 0) {//第一行是介绍
            UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@2", cellId]];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"%@2", cellId]];
                cell.textLabel.text = @"公告类型";
                
                cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
                cell.userInteractionEnabled = NO;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
            }
            cell.detailTextLabel.text = self.notyType;
            return cell;
        }
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.textLabel.text = self.types[indexPath.row -1];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
        }
        return cell;
    }
    

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        KDThirdNotyController *third = [[KDThirdNotyController alloc]init];
        third.delegate = self;
        third.notyNum = [self.notyNmu mutableCopy];
        [self.navigationController pushViewController:third animated:YES];
    }else {
        self.notyType = self.types[indexPath.row -1];
    }
    [self.tableView reloadData];
}

#pragma mark - KDThirdNotyControllerDelegate

- (void)thirdNotyControllerBackToSecondWithInfoArr:(NSArray *)infoArr{
    
    self.notyNmu = [NSString stringWithFormat:@"%ld单位", infoArr.count];
    [self.tableView reloadData];
}

@end
